<template>
  <div>
    <!--  头部  -->
    <header>
      <div>
        <div class="nav-left">
          <!--    logo    -->
          <div class="brand">
            <router-link to="/">
              <img src="../assets/images/logo.png" alt="logo" />
            </router-link>
          </div>
          <el-divider style="height: 50px" direction="vertical"></el-divider>
          <h4>写文章</h4>
          <span v-show="is_save">草稿已保存</span>
        </div>
        <div class="nav-right">
          <div v-if="is_invited">
            <span @click="toggerInvit()">
              <i class="el-icon-document-add"></i>
              邀请预览
            </span>
            <div class="invited-cover" v-show="is_invited_info">
              <i class="el-icon-arrow-up" v-show="is_invited_info"></i>
              <h2>邀请</h2>
              <p>邀请好友来预览这篇草稿，他们将可通过草稿评价给出意见。</p>
              <div>
                <p>相互关注的知乎好友</p>
                <span>+</span>
              </div>
              <el-button class="btn-enter" plain size="mini">确定</el-button>
            </div>
          </div>
          <el-button @click="enterEditValue()" plain size="mini">
            发布
            <i class="el-icon-arrow-down"></i>
          </el-button>
          <div>
            <i @click="toggerMore()" class="el-icon-more"></i>
            <div class="more-cover" v-show="is_more">
              <i class="el-icon-arrow-up" v-show="is_more"></i>
              <ul>
                <li @click="$router.push('./draft')">草稿</li>
                <li>我的文章</li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </header>
    <section>
      <div class="content">
        <div class="up-images">
          <label for="images">
            <img v-if="!img_src" src="../assets/images/camera.png" alt />
            <img v-else :src="'http://lawpro.earnest.pro' + img_src" alt />
            <span>添加题图</span>
            <input
              type="file"
              accept="image/png, image/jpg, image/jpeg"
              name="imageSrc"
              @change="upImage($event)"
              id="images"
            />
          </label>
          <!--     测试     -->
          <div class="show-img">
            <img :src="'@/assets/images/' + filename" alt />
          </div>
        </div>
        <label class="title">
          <input
            type="text"
            v-model="edit_title"
            placeholder="请输入标题（最多50个字）"
          />
        </label>
        <mavon-editor
          :toolbars="toolbar"
          v-model="editValue"
          fontSize="15px"
          :boxShadow="false"
          defaultOpen="edit"
          ref="md"
          @change="changeMavon"
          @save="saveMavon"
          placeholder="请输入正文"
        ></mavon-editor>
        {{ editValue }}
      </div>
    </section>
  </div>
</template>

<script>
export default {
  name: "writing.vue",
  data() {
    return {
      // 正文
      editValue: "",
      edit_title: "",
      img_src: "",
      // 自动保存
      is_save: false,
      // 邀请
      is_invited: false,
      is_invited_info: false,
      // 更多
      is_more: false,
      // 文件名
      filename: "",
      toolbar: {
        bold: true, // 粗体
        italic: true, // 斜体
        header: true, // 标题
        quote: true, // 引用
        ol: true, // 有序列表
        ul: true, // 无序列表
        link: true, // 链接
        imagelink: true, // 图片链接
        underline: false, // 下划线
        strikethrough: false, // 中划线
        mark: false, // 标记
        superscript: false, // 上角标
        subscript: false, // 下角标
        fullscreen: true, // 全屏编辑
        readmodel: true, // 沉浸式阅读
        htmlcode: false, // 展示html源码
        help: true, // 帮助
        /* 1.3.5 */
        trash: true, // 清空
        save: true, // 保存（触发events中的save事件）
        /* 2.1.8 */
        alignleft: true, // 左对齐
        aligncenter: true, // 居中
        alignright: true, // 右对齐
        /* 2.2.1 */
        preview: true // 预览
      }
    };
  },
  created() {},
  watch: {
    editValue() {
      // console.log(this.editValue);
      if (this.editValue) {
        this.is_save = true;
        this.is_invited = true;
      } else {
        this.is_save = false;
      }
    }
  },
  methods: {
    upImage(event) {
      // this.filename = e.target.files[0].name;
      // // 创建全局变量
      // let formData = new FormData();
      // formData.append("file", e.target.files[0]); //键 值 文件名
      // // console.log(formData.get("file"));

      let filetypes = [".jpg", ".png", ".jpeg"];
      let filepath = event.target.value;
      if (filepath) {
        var isnext = false;
        var fileend = filepath.substring(filepath.lastIndexOf("."));
        if (filetypes && filetypes.length > 0) {
          for (var i = 0; i < filetypes.length; i++) {
            if (filetypes[i] == fileend) {
              isnext = true;
              break;
            }
          }
        }
        if (!isnext) {
          alert("不接受此文件类型！");
          return false;
        }
      }
      let filemaxsize = 1024; //1M
      let fileSize = event.target.files[0].size / 1024;
      if (fileSize > filemaxsize) {
        alert("附件大小不能大于" + filemaxsize / 1024 + "M！");
        return false;
      }
      let file = event.target.files[0];
      let param = new FormData();
      param.append("image", file);
      let config = {
        headers: { "Content-Type": "multipart/form-data" }
      };
      this.$axios
        .post("admin/controller/upload", param, config)
        .then(response => {
          let url = response.data.data;
          this.img_src = url;
          console.log(this.img_src);
        });
    },
    saveMavon() {
      console.log(1);
    },
    changeMavon() {
      this.is_save = true;
    },
    toggerInvit() {
      this.is_invited_info = !this.is_invited_info;
    },
    toggerMore() {
      this.is_more = !this.is_more;
    },
    enterEditValue() {
      if (!this.edit_title) {
        this.$message({
          message: "标题不能为空！",
          type: "error",
          center: true,
          customClass: "z-index-tip",
          duration: 2000,
          offset: 20
        });
      } else if (this.editValue.length < 10) {
        this.$message({
          message: "正文至少输入9个字！",
          type: "error",
          center: true,
          customClass: "z-index-tip",
          duration: 2000,
          offset: 20
        });
      } else {
        alert("发布成功？");
      }
    }
  }
};
</script>

<style scoped lang="less">
/*头部*/
header {
  z-index: 9999;
  position: fixed;
  top: 0;
  width: 100%;
  padding: 10px 0;
  background-color: #fff;
  box-shadow: 0 2px 2px 0 rgba(237, 237, 237, 0.4);
  & > div {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 1000px;
    margin: 0 auto;
    padding: 0 15px;
    box-sizing: border-box;
    .nav-left {
      display: flex;
      align-items: center;
      /deep/.el-divider--vertical {
        height: 2rem;
        margin: 0 15px;
      }
      .brand {
        width: 80px;
        img {
          width: 100%;
          height: 100%;
        }
      }
      & > span {
        margin-left: 30px;
        font-size: 14px;
        color: #95998f;
      }
    }
    .nav-right {
      display: flex;
      align-items: center;
      color: #95998f;
      & > div:first-child {
        position: relative;
        margin-right: 20px;
        font-size: 14px;
        cursor: pointer;
        & > span {
          i {
            margin-right: 5px;
          }
        }
        .invited-cover {
          position: absolute;
          top: 38px;
          right: -115px;
          width: 300px;
          padding: 15px;
          box-sizing: border-box;
          border: 1px solid #ccc;
          background-color: #fff;
          .el-icon-arrow-up {
            z-index: 99;
            position: absolute;
            top: -10px;
            right: 140px;
            background-color: #fff;
          }
          & > h2 {
            color: #000;
          }
          & > p {
            margin: 15px 0;
            font-size: 13px;
            line-height: 20px;
            color: #666;
          }
          & > div {
            text-align: center;
            & > p {
              font-size: 13px;
              color: #666;
            }
            & > span {
              width: 50px;
              height: 50px;
              font-size: 40px;
              font-weight: bold;
            }
            .btn-enter {
              float: right;
            }
          }
        }
      }
      & > button {
        color: #95998f;
        margin-right: 30px;
      }
      & > div:last-child {
        position: relative;
        & > i {
          cursor: pointer;
        }
        .more-cover {
          position: absolute;
          top: 38px;
          right: -45px;
          width: 100px;
          padding: 10px 0;
          box-sizing: border-box;
          border: 1px solid #ccc;
          border-radius: 3px;
          background-color: #fff;
          .el-icon-arrow-up {
            z-index: 99;
            position: absolute;
            top: -10px;
            right: 43px;
            background-color: #fff;
          }
          ul {
            li {
              width: 100%;
              padding: 5px 10px;
              font-size: 13px;
              box-sizing: border-box;
              cursor: pointer;
              &:hover {
                background-color: #f1f1f1;
              }
            }
          }
        }
      }
    }
  }
}
section {
  width: 100%;
  height: 100%;
  min-height: 100vh;
  padding-top: 40px;
  margin-top: 55px;
  .content {
    width: 700px;
    margin: 0 auto;
    .up-images > label {
      display: block;
      width: 700px;
      height: 200px;
      text-align: center;
      border-radius: 3px;
      background-color: rgba(0, 0, 0, 0.04);
      cursor: pointer;
      & > img {
        width: 40px;
        height: 40px;
        margin-top: 60px;
      }
      & > span {
        display: block;
        font-size: 15px;
        color: #95998f;
      }
      & > input {
        width: 0;
        height: 0;
        opacity: 0;
      }
    }
    .show-img {
      & > img {
        width: 100px;
        height: 100px;
      }
    }
    .title {
      width: 700px;
      & > input {
        width: 700px;
        height: 60px;
        font-size: 28px;
        &::placeholder {
          font-size: 28px;
          font-weight: bold;
          color: #cacaca;
        }
      }
    }
  }
}
</style>
